package com.example.demo.service.impl;

import com.example.demo.client.SmQryClient;
import com.example.demo.client.evt.cssmqry.menu.UpdatePermissionByParamEvt;
import com.example.demo.mapper.ResourceMapper;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.security.SecurityUtils;
import com.example.demo.service.ResourceService;
import com.example.demo.web.rest.constant.DatabaseConstant;
import com.example.demo.web.rest.evt.*;
import com.example.demo.web.rest.vm.QueryResourceVm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class ResourceServiceImpl implements ResourceService {
    protected Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    protected SmQryClient baseQueryClient;

    @Override
    public QueryPageVm<QueryResourceVm> queryResource(QueryResourceEvt evt) {
        UpdatePermissionByParamEvt permissionByParamEvt = new UpdatePermissionByParamEvt();
        permissionByParamEvt.setType("1");
        permissionByParamEvt.setId("1");
//        baseQueryClient.updatePermissionByParam(permissionByParamEvt);
        if (evt.getPageNo() != null && evt.getPageSize() != null && evt.isCountTotal()) {
            Page page = PageHelper.startPage(evt.getPageNo(), evt.getPageSize(), evt.isCountTotal());
            return QueryPageVm.getInstance(evt, resourceMapper.queryResource(evt), page.getTotal());
        } else {
            return QueryPageVm.getInstance(evt, resourceMapper.queryResource(evt), 0L);
        }
    }

    @Override
    public void saveResource(SaveResourceEvt evt) throws Exception {
        QueryResourceVm vm = new QueryResourceVm();
        BeanUtils.copyProperties(evt,vm);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(new Date());
        String userName = SecurityUtils.getCurrentUserLogin().get();

        vm.setUpdateTime(nowTime);
        vm.setUpdateUserName(userName);
        if(StringUtils.isBlank(vm.getBeanId())){
            vm.setBeanId("");
        }
        if(StringUtils.isBlank(vm.getOperationCode())){
            vm.setOperationCode("[]");
        }

        if(vm.getResourceId() == null){
            TableIdSizeEvt tableIdSizeEvt = new TableIdSizeEvt(DatabaseConstant.TABLE_NAME_RESOURCE_TYPE,DatabaseConstant.ID_SIZE);
            vm.setResourceId(baseQueryClient.getTableId(tableIdSizeEvt));
            vm.setCreateTime(nowTime);
            vm.setCreateUserName(userName);
            resourceMapper.insertResource(vm);
        } else {
            resourceMapper.updateResource(vm);
        }
    }

    @Override
    public void deleteResource(Long resourceId) throws Exception {
        resourceMapper.deleteResource(resourceId);
    }
}
